<?php

class RulePertanyaanTiga extends AbstractRule
{   
    /*
     * 
     * Q = (Q - {kataTanya})
     * 
     * If (Q, {allah})
     * then (Q + {kami})
     * elseif (Q, {kami})
     * then (Q + {kamu})
     * else (Q, Q)
     * 
     * Score (S) += WordMatch (Q, S)
     */
    public function qas($kalimat)
    {
        $kalimat   = strtolower($kalimat);
        $kalimat   = $this->preProcessing->hapusStopwords($kalimat);
        $kalimat   = $this->preProcessing->hapusKarakterKhusus($kalimat);
        $kalimat   = $this->preProcessing->stemming($kalimat);
        $question  = explode(" ", $kalimat);
        $kataTanya = $question[0];

        if ($kataTanya == 'mana') {
            $kataTanya = 'dimana';
        }
        
        //ambil kata tanya
        unset($question[0]);
        
        if (in_array('allah', $question)) {
            array_push($question, 'kami');
        } elseif (in_array('kami', $question)) {
            array_push($question, 'kamu');
        }

        $result      = array();
        $listSura    = array();
        $listAya     = array();
        $listSuraAya = array();
        
        // wordmatch scoring
        $resultWordMatch = array();
        foreach ($question as $kata) {
            $resultWordMatch[$kata] = $this->wordMacthScooring($kata);
        }
        
        //~ var_dump($result);
        
        // memasukkan result wordmatch ke result akhir
        foreach ($resultWordMatch as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $listSura[$data['sura']] = $data['sura'];
                $listAya[$data['aya']]   = $data['aya'];
                $listSuraAya[$data['sura']][$data['aya']] = $data['aya'];

                $result[$data['sura']][$data['aya']]['wordmatch'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['wordmatch'][$kata]['score'] = $data['score'];
            }
        }
        
        //~ var_dump($result);
        
        switch ($kataTanya) {
            case 'apa':
                $result = $this->ruleApa($question, $result, $listSuraAya, $listSura, $listAya);
                break;
            case 'apakah':
                $result = $this->ruleApa($question, $result, $listSuraAya, $listSura, $listAya);
                break;
            case 'siapa':
                $result = $this->ruleSiapa($question, $result, $listSuraAya, $listSura, $listAya);
                break;
            case 'mengapa':
                $result = $this->ruleMengapa($question, $result, $listSuraAya, $listSura, $listAya);
                break;
            case 'kapan':
            //~ var_dump('KAPANNNN');
                $result = $this->ruleKapan($question, $result, $listSuraAya, $listSura, $listAya);
                //~ var_dump($result);
                break;
            case 'dimana':
                $result = $this->ruleDimana($question, $result, $listSuraAya, $listSura, $listAya);
                break;
            default:
                $result = '<div class="alert alert-danger">Pertanyaan harus dimulai dengan kata tanya APA, SIAPA, MENGAPA, KAPAN atau DIMANA</div>';
        }
        
        //~ echo '<strong>SEBELUM</strong><br/>';
        //~ var_dump($result);
        // membuang data yang hanya ada di salah satu
        if (is_array($result)) {
            //~ var_dump('RESULT');
            //~ var_dump($result);
            foreach ($result as $surat => $dataSurat) {
                //~ echo $surat.'<br/>';
                //~ $suratIsKosong = true;
                $suratIsKosong = false;
                foreach ($dataSurat as $ayat => $dataAyat) {
                    //~ echo $ayat.'<br/>';
                    //~ echo $ayat.'-'.count($dataAyat['wordmatch']).'-'.count($dataAyat['rule1']).'-'.count($dataAyat['rule2']).'<br/>';
                    $haveRule = false;
                    if (count($dataAyat['rule1']) > 0 || count($dataAyat['rule2']) > 0 || count($dataAyat['rule3']) > 0 || count($dataAyat['rule4']) > 0) {
                        $haveRule = true;
                    }
                    //~ if (count($dataAyat['wordmatch']) < 1 || count($dataAyat['rule1']) < 1 || count($dataAyat['rule2']) < 1) {
                    if (count($dataAyat['wordmatch']) < 1 || !$haveRule) {
                        unset($result[$surat][$ayat]);
                        //~ unset($listAya[$ayat]);
                        unset($listSuraAya[$surat][$ayat]);
                        $suratIsKosong = true;
                    } else {
                        $suratIsKosong = false;
                    }
                }
                if ($suratIsKosong) {
                    unset($result[$surat]);
                    unset($listSura[$surat]);
                }
            }
            //~ var_dump($result);
        }
        
        //~ echo '<br/><strong>SESUDAH</strong><br/>';
        //~ var_dump($result);

        if (is_array($result) && count($result) >= 1) {
            // total score
            $result = $this->setTotalScoreRule($result);

            // tambah ayat indonesia
            $ayatIndonesia = $this->getAyatIndonesia($listSura, $listAya, $listSuraAya);
            $result = $this->setAyatIndonesia($ayatIndonesia, $result);

            // tambah ayat arabic
            $ayatArabic = $this->getAyatArabic($listSura, $listAya, $listSuraAya);
            $result = $this->setAyatArabic($ayatArabic, $result);

            // sorting berdasarkan total tertinggi
            $result = $this->sortingResult($result);
        }

        // tambah nama surat
        $listNamaSurat = $this->getNamaSurat($listSura);

        $lastResult = array(
            'data'      => $result,
            'namaSurat' => $listNamaSurat,
        );

        return $lastResult;
    }
    
    /*
     * 
     * APA
     *
     * If contains (Q, {maksud}) and
     * contains (S, {adalah, yaitu, ialah})
     * then Score(S) += 20
     * 
     * If contains (S, {adalah, yaitu})
     * then Score(S) += 6
     */
    public function ruleApa(
        array $question,
        array $result,
        array $listSuraAya,
        array $listSura,
        array $listAya
    ) {
        /*
         * If contains (Q, {maksud}) and
         * contains (S, {adalah, yaitu, ialah})
         * then Score(S) += 20
         */
        $containMaksud = (
            in_array('maksud', $question) ||
            in_array('arti', $question) ||
            in_array('definisi', $question) ||
            in_array('devinisi', $question) ||
            in_array('makna', $question) ||
            in_array('terjemah', $question) ||
            in_array('deskripsi', $question)
        );
        if ($containMaksud) {
            $array = array('adalah', 'yaitu', 'ialah', 'yakni', 'merupakan');
            $resultSatu = $this->contains($array, 20, $listSura, $listSuraAya);
            
            foreach ($resultSatu as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['score'] = $data['score'];
                }
            }    
        }

        /*
         * If contains (S, {adalah, yaitu})
         * then Score(S) += 6
         */
        $array = array('adalah', 'yaitu', 'ialah', 'yakni', 'merupakan');
        $resultDua = $this->contains($array, 6, $listSura, $listSuraAya);
        
        foreach ($resultDua as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $result[$data['sura']][$data['aya']]['rule2'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['rule2'][$kata]['score'] = $data['score'];
            }
        }

        return $result;
    }

    /*
     *
     * SIAPA
     *
     * If ~contains (Q, ORANG) and
     * contains (S, ORANG)
     * then Score(S) += 6
     *
     * If contains (Q, ORANG) and
     * contains (S, ORANG)
     * then Score(S) += 20
     */
    public function ruleSiapa(
        array $question,
        array $result,
        array $listSuraAya,
        array $listSura,
        array $listAya
    ) {
        /*
         * If ~contains (Q, ORANG) and
         * contains (S, ORANG)
         * then Score(S) += 6
         */
        $kamusOrang      = $this->getKamusOrang();
        $isContainPeople = $this->containsWords($question, $kamusOrang);
        if (!$isContainPeople) {
            $resultSatu = $this->contains($kamusOrang, 6, $listSura, $listSuraAya);
            
            foreach ($resultSatu as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['score'] = $data['score'];
                }
            }   
        }

        /*
         * If contains (Q, ORANG) and
         * contains (S, ORANG)
         * then Score(S) += 20
         */
        if ($isContainPeople) {
            $resultDua = $this->contains($kamusOrang, 20, $listSura, $listSuraAya);
            
            foreach ($resultDua as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule2'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule2'][$kata]['score'] = $data['score'];
                }
            }   
        }

        return $result;
    }

    /*
     * DIMANA
     *
     * If contains (S, TEMPAT)
     * then Score(S) += 20
     *
     * If contains (S, PREPOSISI_TEMPAT)
     * then Score(S) += 4
     */
    public function ruleDimana(
        array $question,
        array $result,
        array $listSuraAya,
        array $listSura,
        array $listAya
    ) {
        /*
         * If contains (S, TEMPAT)
         * then Score(S) += 20
         */
        $kamusTempat = $this->getKamusTempat();
        $resultSatu  = $this->contains($kamusTempat, 20, $listSura, $listSuraAya);
        
        foreach ($resultSatu as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $result[$data['sura']][$data['aya']]['rule1'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['rule1'][$kata]['score'] = $data['score'];
            }
        }

        /*
         * If contains (S, PREPOSISI_TEMPAT)
         * then Score(S) += 4
         */
        $preposisiTempat = $this->getPreposisiTempat();
        $resultDua = $this->contains($preposisiTempat, 4, $listSura, $listSuraAya);
        
        foreach ($resultDua as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $result[$data['sura']][$data['aya']]['rule2'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['rule2'][$kata]['score'] = $data['score'];
            }
        }

        return $result;
    }

    /*
     * KAPAN
     *
     * If contains (Q, {akhir}) and
     * contains (S, {sejak, akhir})
     * then Score(S) += 20
     *
     * If contains (Q, {mulai, awal}) and
     * contains (S, {sejak, mulai})
     * then Score(S) += 20
     *
     * If contains (S, WAKTU) and
     * contains (S, {pada, selama})
     * then Score(S) += 6
     *
     * If contains (S, WAKTU)
     * then Score(S) += 4
     */
    public function ruleKapan(
        array $question,
        array $result,
        array $listSuraAya,
        array $listSura,
        array $listAya
    ) {
        /*
         * If contains (Q, {akhir}) and
         * contains (S, {sejak, akhir})
         * then Score(S) += 20
         */
        $kataAkhir = array('akhir', 'final', 'penutup', 'selesai', 'tamat');
        $isContainAkhir = $this->containsWords($question, $kataAkhir);
        if ($isContainAkhir) {
            $array = array('sejak', 'semenjak', 'sedari', 'dari', 'akhir', 'final', 'penutup', 'selesai', 'tamat');
            $resultSatu = $this->contains($array, 20, $listSura, $listSuraAya);
            
            foreach ($resultSatu as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule1'][$kata]['score'] = $data['score'];
                }
            }
        }

        /*
         * If contains (Q, {mulai, awal}) and
         * contains (S, {sejak, mulai})
         * then Score(S) += 20
         */
        $kataMulai = array('mulai', 'semenjak', 'sedari', 'dari', 'awal', 'sejak', 'semenjak');
        $isContainAwal = $this->containsWords($question, $kataMulai);
        if ($isContainAwal) {
            $array = array('sejak', 'semenjak', 'sedari', 'dari', 'mulai');
            $resultDua = $this->contains($array, 20, $listSura, $listSuraAya);
            
            foreach ($resultDua as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule2'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule2'][$kata]['score'] = $data['score'];
                }
            }
        }

        /*
         * If contains (S, WAKTU) and
         * contains (S, {pada, selama})
         * then Score(S) += 6
         */
        $kamusWaktu     = $this->getKamusWaktu();
        $isContainWaktu = $this->containsWords($question, $kamusWaktu);
        if ($isContainWaktu) {
            $array = array('pada', 'selama', 'semasa', 'sepanjang', 'ketika', 'selagi', 'sewaktu', 'tatkala');
            $resultTiga = $this->contains($array, 6, $listSura, $listSuraAya);
            
            foreach ($resultTiga as $kata => $dataKata) {
                foreach ($dataKata as $data) {
                    $result[$data['sura']][$data['aya']]['rule3'][$kata]['kata']  = $kata;
                    $result[$data['sura']][$data['aya']]['rule3'][$kata]['score'] = $data['score'];
                }
            }
        }

        /*
         * If contains (S, WAKTU)
         * then Score(S) += 4
         */
        $resultEmpat = $this->contains($kamusWaktu, 6, $listSura, $listSuraAya);
            
        foreach ($resultEmpat as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $result[$data['sura']][$data['aya']]['rule4'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['rule4'][$kata]['score'] = $data['score'];
            }
        }

        return $result;
    }

    /*
     * MENGAPA
     *
     * If contains (S, {agar, karena, supaya, sebab, untuk})
     * then Score(S) += 20
     */
    public function ruleMengapa(
        array $question,
        array $result,
        array $listSuraAya,
        array $listSura,
        array $listAya
    ) {
        /*
         * If contains (S, {agar, karena, supaya, sebab, untuk})
         * then Score(S) += 20
         */
        $array      = array('agar', 'karena', 'supaya', 'sebab', 'untuk', 'lantaran');
        $resultSatu = $this->contains($array, 20, $listSura, $listSuraAya);
            
        foreach ($resultSatu as $kata => $dataKata) {
            foreach ($dataKata as $data) {
                $result[$data['sura']][$data['aya']]['rule1'][$kata]['kata']  = $kata;
                $result[$data['sura']][$data['aya']]['rule1'][$kata]['score'] = $data['score'];
            }
        }

        return $result;
    }
}
